<?php
namespace TennisTournament;

use TennisTournament\Controller\AuthenticationController;
use TennisTournament\Controller\GroupController;
use TennisTournament\Controller\IndexController;
use TennisTournament\Controller\ManagementController;
use TennisTournament\Controller\MatchController;
use TennisTournament\Controller\PlayerController;
use TennisTournament\Persistent\AuthenticationDaoDoctrine;
use TennisTournament\Persistent\GroupDaoDoctrine;
use TennisTournament\Persistent\GroupStatsDaoDoctrine;
use TennisTournament\Persistent\MatchDaoDoctrine;
use TennisTournament\Persistent\PlayerDaoDoctrine;
use TennisTournament\Service\AuthenticationServiceImpl;
use TennisTournament\Service\CommonServiceManager;
use TennisTournament\Service\GroupServiceImpl;
use TennisTournament\Service\GroupStatsServiceImpl;
use TennisTournament\Service\MatchServiceImpl;
use TennisTournament\Service\PlayerServiceImpl;
use TennisTournament\Service\ResultServiceImpl;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e) {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $em = $e->getApplication()->getServiceManager()->get('Doctrine\ORM\EntityManager');
        $platform = $em->getConnection()->getDatabasePlatform();
        $platform->registerDoctrineTypeMapping('enum', 'string');
    }

    public function getConfig() {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig() {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                 __DIR__ . '/autoload_classmap.php'
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getServiceConfig() {
        return array(
            'abstract_factories' => array(
                'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
                'Zend\Log\LoggerAbstractServiceFactory',
            ),
            'aliases' => array(
                'translator' => 'MvcTranslator',
            ),
            'factories' => array(
                'TennisTournament\Service\AuthenticationService' => function($serviceLocator) {
                    return new AuthenticationServiceImpl(
                        $serviceLocator->get('TennisTournament\Persistent\AuthenticationDao')
                    );
                },
                'TennisTournament\Service\CommonService' => function($serviceLocator) {
                    return new CommonServiceManager(
                        $serviceLocator->get('TennisTournament\Persistent\MatchDao')
                    );
                },
                'TennisTournament\Service\GroupService' => function($serviceLocator) {
                    return new GroupServiceImpl(
                        $serviceLocator->get('TennisTournament\Persistent\GroupDao'),
                        $serviceLocator->get('TennisTournament\Service\PlayerService'),
                        $serviceLocator->get('TennisTournament\Service\CommonService')
                    );
                },
                'TennisTournament\Service\GroupStatsService' => function($serviceLocator) {
                    return new GroupStatsServiceImpl(
                        $serviceLocator->get('TennisTournament\Persistent\GroupStatsDao'),
                        $serviceLocator->get('TennisTournament\Service\ResultService')
                    );
                },
                'TennisTournament\Service\ResultService' => function($serviceLocator) {
                    return new ResultServiceImpl();
                },
                'TennisTournament\Service\PlayerService' => function($serviceLocator) {
                    return new PlayerServiceImpl(
                        $serviceLocator->get('TennisTournament\Persistent\PlayerDao')
                    );
                },
                'TennisTournament\Service\MatchService' => function($serviceLocator) {
                    return new MatchServiceImpl(
                        $serviceLocator->get('TennisTournament\Persistent\MatchDao'),
                        $serviceLocator->get('TennisTournament\Service\PlayerService'),
                        $serviceLocator->get('TennisTournament\Service\GroupStatsService'),
                        $serviceLocator->get('TennisTournament\Service\GroupService')
                    );
                },
                'TennisTournament\Persistent\GroupDao' => function($serviceLocator) {
                    return new GroupDaoDoctrine(
                        $serviceLocator->get('Doctrine\ORM\EntityManager')
                    );
                },
                'TennisTournament\Persistent\GroupStatsDao' => function($serviceLocator) {
                    return new GroupStatsDaoDoctrine(
                        $serviceLocator->get('Doctrine\ORM\EntityManager')
                    );
                },
                'TennisTournament\Persistent\PlayerDao' => function($serviceLocator) {
                    return new PlayerDaoDoctrine(
                        $serviceLocator->get('Doctrine\ORM\EntityManager')
                    );
                },
                'TennisTournament\Persistent\MatchDao' => function($serviceLocator) {
                    return new MatchDaoDoctrine(
                        $serviceLocator->get('Doctrine\ORM\EntityManager')
                    );
                },
                'TennisTournament\Persistent\AuthenticationDao' => function($serviceLocator) {
                    return new AuthenticationDaoDoctrine(
                        $serviceLocator->get('Doctrine\ORM\EntityManager')
                    );
                },
            ),
        );
    }

    public function getControllerConfig() {
        return array(
            'factories' => array(
                'TennisTournament\Controller\Player' => function($serviceLocator) {
                    return new PlayerController(
                        $serviceLocator->getServiceLocator()->get('TennisTournament\Service\PlayerService')
                    );
                },
                'TennisTournament\Controller\Management' => function($serviceLocator) {
                    return new ManagementController(
                        $serviceLocator->getServiceLocator()->get('TennisTournament\Service\PlayerService')
                    );
                },
                'TennisTournament\Controller\Authentication' => function($serviceLocator) {
                    return new AuthenticationController(
                        $serviceLocator->getServiceLocator()->get('TennisTournament\Service\AuthenticationService')
                    );
                },
                'TennisTournament\Controller\Index' => function($serviceLocator) {
                    return new IndexController(
                        $serviceLocator->getServiceLocator()->get('TennisTournament\Service\MatchService'),
                        $serviceLocator->getServiceLocator()->get('TennisTournament\Service\PlayerService')
                    );
                },
                'TennisTournament\Controller\Group' => function($serviceLocator) {
                    return new GroupController(
                        $serviceLocator->getServiceLocator()->get('TennisTournament\Service\GroupService'),
                        $serviceLocator->getServiceLocator()->get('TennisTournament\Service\MatchService')
                    );
                },
                'TennisTournament\Controller\Match' => function($serviceLocator) {
                    return new MatchController(
                        $serviceLocator->getServiceLocator()->get('TennisTournament\Service\MatchService')
                    );
                },
            )
        );
    }
}
